define(function(require, exports, module) {
    var layer = require("layer");
    var $ = require("jquery");
    // ajax全局设置
    $.ajaxSetup({
        dataType: "json",
        cache: false,
        layerIndex:-1,
        beforeSend : function(){
            //请求最多10秒关闭
            this.layerIndex = layer.load(0 ,{time: 10000});
        },
        complete: function (xhr, status) {
            layer.close(this.layerIndex);
            try {
                // 通过XMLHttpRequest取得响应头，REDIRECT
                var redirect = xhr.getResponseHeader("REDIRECT");
                if (redirect == "REDIRECT") {
                    var win = window;
                    while (win != win.top) {
                        win = win.top;
                    }
                    layer.confirm($.i18n.prop('session.timeout.tips'), {
                        area: ['450px'],
                        btn: [$.i18n.prop('session.timeout.yes.btn'), $.i18n.prop('session.timeout.cancel.btn')]
                    }, function (index) {
                        layer.close(index);
                        win.location.href = xhr.getResponseHeader("CONTEXTPATH");
                    });
                    return;
                }
                //非法参数提示
                if(xhr.status == 400){
                    layer.alert(xhr.responseText);
                }

            } catch (e) {
            }
        }
    });
    /**
     * ajax统一封装后对象
     * @param type
     * @param url
     * @param data
     * @param options
     * @constructor
     */
    var Ajax = function(type , url , data , options){
        this.settings = {
            type : type ,
            url : ajaxBaseUrl + url ,
            data : data,
            dataType : "json"
        };
        for(var key in options){
            this.settings[key] = options[key];
        }
    };
    /**
     * ajax执行成功后方法
     * @param response
     * @returns {Ajax}
     */
    Ajax.prototype.then = function(response){
        var _this = this;
        _this.settings.success = function (data){
            if(response){
                response(data);
            }
        };
        return this;
    };
    /**
     * ajax执行出错后方法
     * @param exception
     * @returns {Ajax}
     */
    Ajax.prototype.error = function(exception){
        var _this = this;
        _this.settings.error = function (error){
            if(exception){
                exception(error);
            }
        };
        return this;
    };
    /**
     * 执行
     * @param async true表示异步 false表示同步
     */
    Ajax.prototype.execute = function(async){
        var _this = this;
        _this.settings.async = async;
        $.ajax(_this.settings);
    };

    $.http = {};

    $.http.post = function(url , data , options){
        var ajax = new Ajax("post" , url , data , options);
        return ajax;
    };

    $.http.get = function(url , data , options){
        var ajax = new Ajax("get" , url , data , options);
        return ajax;
    };

    $.http.put = function(url , data , options){
        var ajax = new Ajax("put" , url , data , options);
        return ajax;
    };

    //由于delete在ie8是关键字不能用，所有用del来代替
    $.http.del = function(url , data , options){
        var ajax = new Ajax("delete" , url , data , options);
        return ajax;
    };
});
